package org.xiyu.yee.capemanage.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.logging.LogUtils;
import org.slf4j.Logger;
import org.xiyu.yee.capemanage.Capemanage;
import org.xiyu.yee.capemanage.data.PlayerCapeData;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;

import java.io.*;
import java.util.List;
import java.util.Random;
import java.util.Arrays;
import java.util.stream.Collectors;

public class CapeManager {
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
    private static final String DEFAULT_CAPE = "default";
    private static final List<String> AVAILABLE_CAPES = Arrays.asList(
        "1m",                    // 1mcape.png
        "bugtracker",           // bugtracker_cape.png
        "bacon",                // bacon_cape.png
        "15year",               // 15year_texture.png
        "cherry",               // cherry_cape_texture.png
        "translator_chinese",   // chinese_translator_cape.png
        "translator",           // crowdin_translator_cape.png
        "db",                   // db_cape.png
        "cobalt",               // cobalt_cape.png
        "eyeblossom",          // eyeblossom_texture.png
        "followers",            // followers_cape_texture.png
        "julian",               // julian_clark.png
        "realms_mapmaker",      // mapmaker_cape_texture.png
        "mcc",                  // mc_championship_texture.png
        "minecon_2016",         // minecon_2016_cape.png
        "minecon_2011",         // minecon_2011_cape.png
        "minecon_2015",         // minecon_2015_cape_texture.png
        "experience",           // minecraft_experience_rescue_texture.png
        "minecon_2012",         // minecon_2012_cape.png
        "mojang_classic",       // mojang_cape.png
        "mojang_2015",          // mojang_cape_2015.png
        "mrmessiah",            // mrmessiah_2d.png
        "mojang_studios",       // ms_cape.png
        "minecon_2013",         // minecon_2013_cape.png
        "party",                // party_cape.png
        "prismarine",           // prismarine_cape.png
        "purple_heart",         // purple_heart_cape.png
        "scrolls",              // scrolls_cape.png
        "turtle",               // turtle_cape.png
        "vanilla",              // vanilla_cape.png
        "valentine",            // valentine_cape.png
        "volgar"                // volgar_cape.png
    );
    
    public static void saveCapeData(File file, PlayerCapeData data) {
        if (data == null) {
            LOGGER.error("Attempted to save null PlayerCapeData");
            return;
        }
        
        try {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            
            try (Writer writer = new FileWriter(file)) {
                GSON.toJson(data, writer);
            }
        } catch (IOException e) {
            LOGGER.error("Failed to save cape data for player: " + data.getPlayerName(), e);
        }
    }
    
    public static PlayerCapeData loadCapeData(File file) {
        if (!file.exists()) {
            LOGGER.debug("Cape data file does not exist: {}", file.getPath());
            return null;
        }
        
        try (Reader reader = new FileReader(file)) {
            PlayerCapeData data = GSON.fromJson(reader, PlayerCapeData.class);
            return data;
        } catch (IOException e) {
            LOGGER.error("Failed to load cape data from file: " + file.getPath(), e);
            return null;
        }
    }
    
    public static String getRandomCape() {
        return AVAILABLE_CAPES.get(new Random().nextInt(AVAILABLE_CAPES.size()));
    }
    
    public static boolean isCapeValid(String capeName) {
        if (capeName.startsWith("egg.")) {
            return new ResourceLocation(Capemanage.MODID, 
                "textures/capes/egg/" + capeName.substring(4) + ".png").getPath() != null;
        }
        return AVAILABLE_CAPES.contains(capeName);
    }
    
    public static List<String> getAvailableCapes() {
        return AVAILABLE_CAPES;
    }
} 